<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <div class="block">
          <el-date-picker
            v-model="dataForm.shippingDate"
            type="date"
            format="yyyy-MM-dd"
            placeholder="发货日期">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.salesman" placeholder="业务员" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.customerName" placeholder="客户名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.documentType" placeholder="发货/退货" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.number" placeholder="编号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.productName" placeholder="产品名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.region" placeholder="所属区域" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.invoiceCompany" placeholder="开票公司" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('cw:receivablesummary:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('cw:receivablesummary:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
      <el-form-item>  
        <el-upload
            class="upload-demo"
            ref="upload"
            action=""
            :http-request="submitUpload"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-change='onChange'
            :file-list="fileList"
            :auto-upload="true">
            <el-button  type="primary" >数据导入</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <el-table
      v-el-table-infinite-scroll="load"
      :data="dataList"
      :row-key="id"
      :infinite-scroll-disabled="disabled"
      border
      show-summary
      :summary-method="getSummaries"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="shippingDate"
        header-align="center"
        align="center"
        label="发货日期">
      </el-table-column>
      <el-table-column
        prop="salesman"
        header-align="center"
        align="center"
        label="业务员">
      </el-table-column>
      <el-table-column
        prop="customerName"
        header-align="center"
        align="center"
        label="客户名称">
      </el-table-column>
      <el-table-column
        prop="documentType"
        header-align="center"
        align="center"
        label="发货/退货">
      </el-table-column>
      <el-table-column
        prop="number"
        header-align="center"
        align="center"
        label="编号">
      </el-table-column>
      <el-table-column
        prop="productName"
        header-align="center"
        align="center"
        label="产品名称">
      </el-table-column>
      <el-table-column
        prop="quantity"
        header-align="center"
        align="center"
        label="数量">
      </el-table-column>
      <el-table-column
        prop="unitPrice"
        header-align="center"
        align="center"
        label="单价">
      </el-table-column>
      <el-table-column
        prop="invoiceAmount"
        header-align="center"
        align="center"
        label="金额_开票">
      </el-table-column>
      <el-table-column
        prop="region"
        header-align="center"
        align="center"
        label="所属区域">
      </el-table-column>
      <el-table-column
        prop="invoiceCompany"
        header-align="center"
        align="center"
        label="开票公司">
      </el-table-column>
      <el-table-column
        prop="settlementPrice"
        header-align="center"
        align="center"
        label="结算价">
      </el-table-column>
      <el-table-column
        prop="guidePrice"
        header-align="center"
        align="center"
        label="指导价">
      </el-table-column>
      <el-table-column
        prop="commission"
        header-align="center"
        align="center"
        label="提成">
      </el-table-column>
      <el-table-column
        prop="dueDate"
        header-align="center"
        align="center"
        label="到期时间">
      </el-table-column>
      <el-table-column
        prop="overdueDays"
        header-align="center"
        align="center"
        label="超期天数">
      </el-table-column>
      <el-table-column
        prop="bank"
        header-align="center"
        align="center"
        label="回款银行">
      </el-table-column>
      <el-table-column
        prop="paymentDate"
        header-align="center"
        align="center"
        label="回款日期">
      </el-table-column>
      <el-table-column
        prop="publicPaymentAmount"
        header-align="center"
        align="center"
        label="对公回款金额">
      </el-table-column>
      <el-table-column
        prop="unitReceivable"
        header-align="center"
        align="center"
        label="单位应收款">
      </el-table-column>
      <el-table-column
        prop="salesmanPaymentDate"
        header-align="center"
        align="center"
        label="业务员回款日期">
      </el-table-column>
      <el-table-column
        prop="personalOthers"
        header-align="center"
        align="center"
        label="个人其他">
      </el-table-column>
      <el-table-column
        prop="salesmanPayment"
        header-align="center"
        align="center"
        label="业务员回款">
      </el-table-column>
      <el-table-column
        prop="salesmanReceivable"
        header-align="center"
        align="center"
        label="业务员应收">
      </el-table-column>
      <el-table-column
        prop="salesmanAdvance"
        header-align="center"
        align="center"
        label="业务员代垫">
      </el-table-column>
      <el-table-column
        prop="chargeAmount"
        header-align="center"
        align="center"
        label="冲账金额">
      </el-table-column>
      <el-table-column
        prop="advanceDate"
        header-align="center"
        align="center"
        label="代垫日期">
      </el-table-column>
      <el-table-column
        prop="exchangeRate"
        header-align="center"
        align="center"
        label="汇率">
      </el-table-column>
      <el-table-column
        prop="exchange"
        header-align="center"
        align="center"
        label="汇兑">
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[50,100,1000]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './receivablesummary-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        members: [],
        sum: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/cw/receivablesummary/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'dataForm': this.dataForm.shippingDate,
            'salesman': this.dataForm.salesman,
            'customerName': this.dataForm.customerName,
            'documentType': this.dataForm.documentType,
            'number': this.dataForm.number,
            'productName': this.dataForm.productName,
            'region': this.dataForm.region,
            'invoiceCompany': this.dataForm.invoiceCompany
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
            this.sum = data.sum
          } else {
            this.dataList = []
            this.totalPage = 0
            this.sum = []
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/cw/receivablesummary/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      getSummaries (param) {
        const { columns } = param
        const sums = []
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '总价'
            return
          }
          switch (column.property) {
            case 'quantity' :
              sums[index] = this.sum.quantitySum
              break
            case 'unitPrice' :
              sums[index] = this.sum.unitPriceSum
              break
            case 'invoiceAmount' :
              sums[index] = this.sum.invoiceAmountSum
              break
            default :
              break
          }
        })

        return sums
      },
      // 选取完文件触发事件
      onChange (a, fileList) {
        this.fileList = fileList
      },
      submitUpload () {
        var _this = this
        var param = new FormData()
        console.log(_this.fileList)
        param.append('file', _this.fileList[0].raw)

        _this.$http({
          url: this.$http.adornUrl('/cw/receivablesummary/readFromExcel'),
          method: 'post',
          data: param,
          contentType: 'multipart/form-data'
        }).then(() => {
          console.log('导入成功')
        })
      }
    }
  }
</script>
